// CouponActivity.java
package com.example.hlddz2.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.hlddz2.R;
import java.util.ArrayList;
import java.util.List;

public class CouponActivity extends AppCompatActivity {
    private ListView lvCoupon;
    private CouponAdapter adapter;
    private List<Coupon> couponList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon);

        initData();
        lvCoupon = findViewById(R.id.lv_coupon);
        adapter = new CouponAdapter();
        lvCoupon.setAdapter(adapter);
    }

    private void initData() {
        couponList = new ArrayList<>();
        couponList.add(new Coupon("满100减20", "有效期至2025-12-31", "全场通用"));
        couponList.add(new Coupon("满200减50", "有效期至2025-11-30", "数码类专用"));
        couponList.add(new Coupon("满500减100", "已过期", "全场通用", true));
    }

    class Coupon {
        String value;
        String date;
        String type;
        boolean isExpired;

        public Coupon(String value, String date, String type) {
            this(value, date, type, false);
        }

        public Coupon(String value, String date, String type, boolean isExpired) {
            this.value = value;
            this.date = date;
            this.type = type;
            this.isExpired = isExpired;
        }
    }

    class CouponAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return couponList.size();
        }

        @Override
        public Object getItem(int position) {
            return couponList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(CouponActivity.this, R.layout.item_coupon, null);
            Coupon coupon = couponList.get(position);

            TextView tvValue = view.findViewById(R.id.tv_value);
            TextView tvDate = view.findViewById(R.id.tv_date);
            TextView tvType = view.findViewById(R.id.tv_type);

            tvValue.setText(coupon.value);
            tvDate.setText(coupon.date);
            tvType.setText(coupon.type);

            // 过期优惠券置灰
            if (coupon.isExpired) {
                tvValue.setTextColor(getResources().getColor(R.color.gray_500));
                tvDate.setTextColor(getResources().getColor(R.color.gray_500));
                tvType.setTextColor(getResources().getColor(R.color.gray_500));
            }
            return view;
        }
    }
}